﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SpyApp.Models
{
    public class CustomPageList<T> : CustomPageList
    {
        public CustomPageList(IEnumerable<T> listFull, int pageIndex, int pageSize)
        {
            if (pageIndex < 1)
                throw new ArgumentOutOfRangeException("pageNumber", pageIndex, "PageNumber cannot be below 1.");
            if (pageSize < 1)
                throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");

            // set source to blank list if superset is null to prevent exceptions
            this.TotalItemCount = listFull == null ? 0 : listFull.Count();
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;

            this.PageCount = TotalItemCount > 0
                            ? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
                            : 0;
            this.HasPreviousPage = PageIndex > 1;
            this.HasNextPage = PageIndex < PageCount;
            this.IsFirstPage = PageIndex == 1;
            this.IsLastPage = PageIndex >= PageCount;
            this.FirstItemOnPage = (PageIndex - 1) * PageSize + 1;

            var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1;
            this.LastItemOnPage = numberOfLastItemOnPage > TotalItemCount
                            ? TotalItemCount
                            : numberOfLastItemOnPage;

            // add items to internal list
            if (TotalItemCount > 0)
            {
                if (pageIndex == 1)
                {
                    Skip = 0;
                }
                else
                {
                    Skip = (pageIndex - 1) * PageSize;
                }
                this.ListCustom = listFull.Skip(Skip).Take(PageSize);
            }
        }

        public IEnumerable<T> ListCustom { get; set; }
    }

    public class CustomPageList
    {
        public bool HasPreviousPage { get; set; }
        public bool HasNextPage { get; set; }
        public bool IsFirstPage { get; set; }
        public bool IsLastPage { get; set; }
        public int TotalItemCount { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int PageCount { get; set; }
        public int FirstItemOnPage { get; set; }
        public int LastItemOnPage { get; set; }
        public int Skip { get; set; }
    }

    public class CustomPageListRenderOption
    {
        public CustomPageListRenderOption()
        {
            ContainClasses = "pagination";
            ItemClasses = "";
            ActiveClasses = "active";

            NextPageFormat = ">";
            PreviousPageFormat = "<";
            CurrentPageFormat = "{0}";
            GapPageFormat = "...";
            PageFormat = "{0}";
            //display form [10] to [20] of [200] items
            ItemInfoFormat = "{0}->{1}/{2} items";
        }

        public string ContainClasses { get; set; }

        public string ItemClasses { get; set; }

        public string NextPageFormat { get; set; }

        public string PreviousPageFormat { get; set; }

        public string CurrentPageFormat { get; set; }

        public string GapPageFormat { get; set; }

        public string PageFormat { get; set; }

        public string ActiveClasses { get; set; }

        public string ItemInfoFormat { get; set; }
    }
}